#xgeom parser

import os, sys
from Sanae3D.lib.ModelObject import Model3D
from Sanae3D.lib.StructReader import StructReader

class Argo_xgeom(Model3D):
    
    IMG_FORMAT = ".png"
    
    def __init__(self, obj3D=None, inFile=None, outFile=None):
        '''Initialize an XAC self.inFile.readect.'''
        
        super(Argo_xgeom, self).__init__("xgeom")
        self.obj3D = obj3D
        self.inFile = inFile
        self.outFile = outFile
        
    def parse_matlib(self, libName, path):
    
        try:
            matLib = StructReader(open(os.path.join(path, libName), 'rb'))
        except:
            print "Couldn't find material library: %s" %libName
            return -1
        
        magic = matLib.read_string(4)
        matLib.read(10)
        matNum = 0
        
        for i in range(matNum + 1):
            charCount = matLib.read_short()
            matName = matLib.read_string(charCount)
        
            self.create_material(i)
            self.add_material_name(i, matName)
            self.add_texture_name(i, matName + self.IMG_FORMAT)
        
    def parse_file(self, path):
        
        magic = self.inFile.read_string(4)
        
        if magic == "xbin":
            self.inFile.read(10)
            
            charCount = self.inFile.read_short()
            meshName = self.inFile.read_string(charCount)
            self.create_object(meshName)
            self.inFile.read(10)
            charCount = self.inFile.read_short()
            uniqueID = self.inFile.read_string(charCount)
            self.inFile.read(2)
            vertType = self.inFile.read_short()
            
            self.inFile.read(5)
            vertCount = self.inFile.read_short()
            faceCount = self.inFile.read_short()
            
            #type 1 vertSize: 32
            #type 3 vertSize: 48
            print vertType
            
            for i in range(vertCount):
                vx, vy, vz = self.inFile.read_float(3)
                nx, ny, nz = self.inFile.read_float(3)
                tu, tv = self.inFile.read_float(2)
                
                if vertType == 3:
                    self.inFile.read_float(4)
                elif vertType == 4:
                    self.inFile.read_float(6)
                
                vx = float(vx) * -1
                self.create_vertex(meshName, i)
                self.add_coords(meshName, i, [vx, vy, vz])
                self.add_vert_normals(meshName, i, [nx, ny, nz])
                self.add_vert_uv(meshName, i, [tu, tv])
            
            for i in range(faceCount / 3):
                f1, f2, f3 = self.inFile.read_short(3)
                
                self.create_face(meshName, i)
                self.add_face_verts(meshName, i, [f1, f2, f3])
                self.add_face_material(meshName, i, 0)
            
            print "%d faces, %d vertices" %(faceCount, vertCount)
            
            self.inFile.read_short()
            charCount = self.inFile.read_short()
            matLib = self.inFile.read_string(charCount)
            self.parse_matlib(matLib, path)
        else:
            print "Unsupported format: %s" %magic
    
def read_file(path):
    
    openFile = StructReader(open(path, 'rb'))
    path = os.path.dirname(path)
    obj = Argo_xgeom(inFile=openFile)
    obj.parse_file(path)
    openFile.close()
    return obj

def definitions():
    
    return "xbin", "XGEOM", "Argo xgeom"